#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Function.h"
 
void Add(struct member *add,int use_detail) //添加联系人 
{
	int num;  	//用于记录序号 
	char choose[100];		//用于记录选项 		
	int key = 1;		//函数能正常进行的“钥匙” 

	while(key==1)
	{
		while(key==1)
		{
			printf("\n请输入你想要的序号(1~100)：\n");
			scanf("%d",&num);
			key = 0;
			
			if((num<1)||(num>100))  //不可以输入其他序号 
			{
				printf("请重新输入！\n");
				key = 1;
			}
			
			if((num>=1)&&(num<=100)) 
			{
				if((add[num].isuse!=0)&&(key!=1)) //覆盖联系人 
				{
					printf("抱歉，该序号已被占用，是否将其覆盖？\n"); 
					printf("1.继续\n");
					printf("2.返回上一步\n");
					scanf("%s",choose);
					if(strcmp(choose,"1")==0)
					{
						key = 2;
					} 
					
					else if(strcmp(choose,"2")==0)
					{
						key = 1;
					} 
					else
					{
						printf("请重新输入~\n");
						key = 1;
					}
				}
				
				if((key==0)&&(use_detail==1))
				{
					printf("1.继续\n");
					printf("2.返回上一步\n");
					scanf("%s",choose);
					if(strcmp(choose,"1")==0) 
					{
						key = 2;
					}
					
					else if(strcmp(choose,"2")==0)
					{
						key = 1;
					} 
					else
					{
						printf("请重新输入~\n");
						key = 1;
					}
				}	
				if(key!=1) 
				{
					if((use_detail==0)&&(add[num].isuse!=1))
					{
						key = 2;
					}
				}
			}
			
			
		}
		
		while(key==2) //输入名字 
		{
			printf("\n请输入他的名字：\n");
			scanf("%s",&add[num].name);
			if(use_detail==1)
			{
				printf("\n是否继续？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if(strcmp(choose,"1")==0)
				{
					key = 3;
				} 
				else if(strcmp(choose,"2")==0) 
				{
					key = 2;
				}
				else 
				{
					printf("您输入的有问题，请重试\n");
					key = 2;
				}
			}
			
			if(use_detail==0)
			{
				key = 3;
			}
			
			
		}
		
		while(key==3) //输入职务 
		{
			printf("请在输入他的职务：\n");
			scanf("%s",&add[num].job);
			if(use_detail==1)
			{
				printf("\n是否继续？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if(strcmp(choose,"1")==0) 
				{
					key = 4;
				}
				else if(strcmp(choose,"2")==0) 
				{
					key = 3;
				}
				else 
				{
					printf("您输入的有问题，请重试\n");
					key = 3;
				}
			}
			
			if(use_detail==0)
			{
				key = 4;
			}
			
		}
		
		while(key==4) //输入职称 
		{
			printf("\n请在输入他的职称：\n");
			scanf("%s",&add[num].rank);
			if(use_detail==1)
			{
				printf("\n是否继续？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if(strcmp(choose,"1")==0) 
				{
					key = 5;
				}
				else if(strcmp(choose,"2")==0) 
				{
					key = 4;
				}
				else 
				{
					printf("您输入的有问题，请重试\n");
					key = 4;
				}
			}
			
			if(use_detail==0)
			{
				key = 5;
			}
		
		}
		
		while(key==5)  //输入手机号 
		{
			printf("\n请在输入他的手机号：\n");
			scanf("%s",&add[num].tele);
			if(use_detail==1)
			{
				printf("\n是否继续？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if(strcmp(choose,"1")==0) 
				{
					key = 6;
				}
				else if(strcmp(choose,"2")==0) 
				{
					key = 5;
				}
				else 
				{
					printf("您输入的有问题，请重试\n");
					key = 5;
				}
			}
			
			if(use_detail==0)
			{
				key = 6;
			}
			
		}
		
		while(key==6) //输入邮箱 
		{
			printf("\n请在输入他的邮箱：\n");
			scanf("%s",&add[num].email);
			if(use_detail==1)
			{
				printf("\n是否继续？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if(strcmp(choose,"1")==0) 
				{
					key = 1;
				}
				else if(strcmp(choose,"2")==0) 
				{
					key = 6;
				}
				else 
				{
					printf("您输入的有问题，请重试\n");
					key = 6;
				}
			}
			
			if(use_detail==0)
			{
				key = 1;
			}
		}
		
		system("cls");
		add[num].isuse=1;
		printf("您添加成功，该联系人将位于序号：%d\n",num);
		key = 2;
	}
}

void Update(struct member *update)  //修改联系人信息 
{
	char choose[100];  	//用于记录选择 
	int num; 		//用于记录序号 
	int key = 1;	//函数能正常进行的“钥匙” 
	
	while(key==1)
	{
		printf("请输入你需要修改的序号：(1~100)\n");
		scanf("%d",&num);
		key = 2;
		if((num<1)||(num>100)) 
		{
			printf("请重新输入！\n"); //禁止输入未定义的序号 
			key = 1;
		}
		if(key==2)   //修改信息 
		{
			printf("请选择您需要修改的类别：\n");
			printf("1.名称\n");
			printf("2.职务\n");
			printf("3.职称\n");
			printf("4.手机号\n");
			printf("5.邮箱\n");
			scanf("%s",choose);
			if(strcmp(choose,"1")==0)
			{
				printf("原名称：%s\n",update[num].name);
				printf("请输入新名称：");
				scanf("%s",&update[num].name);
				system("cls");
				printf("恭喜您修改成功！\n");
				key = 2;
			}
			else if(strcmp(choose,"2")==0)
			{
			 	printf("原职务：%s\n",update[num].job);
				printf("请输入新职务：");
				scanf("%s",&update[num].job);
				system("cls");
				printf("恭喜您修改成功！\n");
				key = 2;
			}
			else if(strcmp(choose,"3")==0) 
			{
		  		printf("原职称：%s\n",update[num].rank);
				printf("请输入新职称：");
				scanf("%s",&update[num].rank);
				system("cls");
				printf("恭喜您修改成功！\n");
				key = 2;
			}
			else if(strcmp(choose,"4")==0)
			{
		 		printf("原手机号：%s\n",update[num].tele);
				printf("请输入新手机号：");
				scanf("%s",&update[num].tele);
				system("cls");
				printf("恭喜您修改成功！\n");
				key = 2;
			} 
			else if(strcmp(choose,"5")==0)
			{
				printf("原邮箱：%s\n",update[num].email);
				printf("请输入新邮箱：");
				scanf("%s",&update[num].email);
				system("cls");
				printf("恭喜您修改成功！\n");
				key = 2;
			}
			else
			{
				printf("请重新输入！\n");
				key = 1; 
			}	
		}
		
	}
}

void Delete(struct member *deletE)  //删除联系人的某个信息 
{
	char none[40] = "none"; 	//删除时将被删除物改为none 
	char choose[100];		//记录选择 
	char son_choose[100];	//子选择 
	int num;		//记录序号 
	int key = 1;	//函数能正常进行的“钥匙” 
	
	while(key==1)
	{
		printf("请输入你需要删除的序号：(1~100)\n"); //禁止输入未定义的序号 
		scanf("%d",&num);
		key = 2;
		if((num<1)||(num>100)) 
		{
			printf("请重新输入！\n");
			key = 1;
		}
		if(key==2)
		{
			printf("请选择您需要删除的类别：\n"); //选择需要删除的类别
			printf("1.名称\n");
			printf("2.职务\n");
			printf("3.职称\n");
			printf("4.手机号\n");
			printf("5.邮箱\n");
			scanf("%s",choose);
			if(strcmp(choose,"1")==0) //删除名称 
			{ 
				printf("原名称：%s\n",deletE[num].name);
				printf("请问是否要删除？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0) 
				{
					strcpy(deletE[num].name,none);
				}
				if(strcmp(son_choose,"2")==0) 
				{
					key = 2;
				}
				system("cls");
				printf("恭喜您删除成功！\n");
				key = 2;
			}
			else if(strcmp(choose,"2")==0) //删除职务
			{
			 	printf("原职务：%s\n",deletE[num].job);
				printf("请问是否要删除？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0) 
				{
					strcpy(deletE[num].job,none);
				}
				if(strcmp(son_choose,"2")==0) 
				{
					key = 2;
				}
				system("cls");
				printf("恭喜您删除成功！\n");
				key = 2;
			}
			else if(strcmp(choose,"3")==0)  //删除职称
			{
			  	printf("原职称：%s\n",deletE[num].rank);
				printf("请问是否要删除？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0) 
				{
					strcpy(deletE[num].rank,none);
				}
				if(strcmp(son_choose,"2")==0) 
				{
					key = 2;
				}
				system("cls");
				printf("恭喜您删除成功！\n");
				key = 2;
			  }
			else if(strcmp(choose,"4")==0) //删除手机号
			{
			 	printf("原手机号：%s\n",deletE[num].tele);
				printf("请问是否要删除？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0) 
				{
					strcpy(deletE[num].tele,none);
				}
				if(strcmp(son_choose,"2")==0) 
				{
					key = 2;
				}
				system("cls");
				printf("恭喜您删除成功！\n");
				key = 2;
			} 
			else if(strcmp(choose,"5")==0) //删除邮箱
			{
				printf("原邮箱：%s\n",deletE[num].email);
				printf("请问是否要删除？\n");
				printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0) 
				{
					strcpy(deletE[num].email,none);
				}
				if(strcmp(son_choose,"2")==0) 
				{
					key = 2;
				}
				system("cls");
				printf("恭喜您删除成功！\n");
				key = 2;
			}
			else
			{
				printf("请重新输入！\n");
				key = 1;
			}
		}
	}
}

void Inquire(struct member *inquire,int *once) //此函数用于查询 
{		
	char choose[100];		//父选项 
	char son_choose[100];	//子选项 
	int enter; 		//用于在显示时计数回车 
	int num;	//用于冒泡排序 
	int numm;	//用于冒泡排序 
	char none[100] = "none";
	int ishave;		//用于验证是否确有此人并且信息重复时优先显示第一人的信息 
	char temp[50];	//用于字母排序中的冒泡排序 
	char name[101][50]; //制作所有联系人的副本 
	int key = 1;		//函数能正常进行的“钥匙” 
	
	while(key==1)
	{
		printf("请选择您需要的方法：\n");
		printf("1.查看列表\n");
		printf("2.分类查找\n");
		printf("3.返回主菜单\n");
		scanf("%s",choose);
		
		if(strcmp(choose,"1")==0)
		{
			printf("请选择你需要的方法：\n");
			printf("1.按序号排序\n");
			printf("2.按字母排序\n");
			printf("3.返回上一步\n");
			scanf("%s",son_choose);
			if(strcmp(son_choose,"1")==0)  //按序号排序 
			{
				printf("以下为所有联系人名称：\n"); //按序号排名输出所有的人名 
				for(num = 1,enter = 1; num<101; num++,enter++)
				{
					printf("%3d.%10s     ",num,inquire[num].name);
					if(enter%5==0) 
					{
						printf("\n");
					}
				}
				printf("\n请选择你要找的人的序号：\n");
				scanf("%d",&num);
				printf("\n以下为该联系人的全部信息：\n"); //弹出这个人的全部信息 
				printf("姓名：%s\n",inquire[num].name);
				printf("职务：%s\n",inquire[num].job);
				printf("职称：%s\n",inquire[num].rank);
				printf("手机：%s\n",inquire[num].tele);
				printf("邮箱：%s\n",inquire[num].email);
				printf("输入任意键继续~\n");
				scanf("%s",choose);
				system("cls");
				key = 2;
			} 
			else if(strcmp(son_choose,"2")==0)  //按字母排序
			{
				printf("以下为所有联系人名称：\n");
				for(num = 1; num<101; num++) 
				{
					strcpy(name[num],inquire[num].name);
				}
				
				for(num = 1; num<200; num++)	//冒泡排序 
				{
					for(numm = 1; numm<=99; numm++)
					{
						if(strcmp(name[numm],none)==0||strcmp(name[numm+1],none)!=0)
						{
							if(name[numm][0]>=name[numm+1][0])
							{
								strcpy(temp,name[numm]);
								strcpy(name[numm],name[numm+1]);
								strcpy(name[numm+1],temp);
							}

                            if(strcmp(name[numm],none)==0)
					    	{
						    	strcpy(temp,name[numm]);
						    	strcpy(name[numm],name[numm+1]);
						    	strcpy(name[numm+1],temp);
						    }
						}
						
						
					}
				}
				
				for(num = 1, enter = 1; num<101; num++,enter++)
				{
					printf("%3d.%10s     ",num,name[num]);
					if(enter%5==0) 
					{
						printf("\n");
					}
				}
				
				printf("\n请选择你要找的人的序号：\n");
				scanf("%d",&num);
				*once = 0;
				
				for(numm = 1; numm<101; numm++)
				{
					if((strcmp(inquire[numm].name,name[num])==0)&&(*once==0) )
					{
						printf("\n以下为该联系人的全部信息：\n");
						printf("姓名：%s\n",inquire[numm].name);
						printf("职务：%s\n",inquire[numm].job);
						printf("职称：%s\n",inquire[numm].rank);
						printf("手机：%s\n",inquire[numm].tele);
						printf("邮箱：%s\n",inquire[numm].email);
						*once = 1;
					}
				}
				key = 2;
				printf("输入任意键继续~\n");
				scanf("%s",choose);
				system("cls");
			}
			else if(strcmp(son_choose,"3")==0) 
			{
				key = 1; 
				
			}
	 	} 
	 	else if(strcmp(choose,"2")==0)
	 	{
	 		int num;		//序号 
	 		char evertype[50];
	 		char chinese[50] ;		//显示的文字 
	 		char information[101][50];  	//拷贝需要的信息 
	 		printf("请选择你要查询的类别：\n");
	 		printf("1.名称\n");
			printf("2.职务\n");
			printf("3.职称\n");
			printf("4.手机号\n");
			printf("5.邮箱\n");
			scanf("%s",son_choose);
			
			if(strcmp(son_choose,"1")==0) 	//该if语句用于更换类别 
			{
				char name[50] = "名字";
				strcpy(chinese,name);
				for(num = 1; num<101; num++) 
				{
					strcpy(information[num],inquire[num].name);
				}	
			}
			else if(strcmp(son_choose,"2")==0)
			{
				char name[50] = "职务";
				strcpy(chinese,name);
				for(num = 1; num<101; num++)
				{
					strcpy(information[num],inquire[num].job);
				}	
			}
			else if(strcmp(son_choose,"3")==0)
			{
				char name[50] = "职称";
				strcpy(chinese,name);
				for(num = 1; num<101; num++)
				{
					strcpy(information[num],inquire[num].rank);
				}	
			}
			else if(strcmp(son_choose,"4")==0)
			{
				char name[50] = "手机号";
				strcpy(chinese,name);
				for(num = 1; num<101; num++)
				{
					strcpy(information[num],inquire[num].tele);
				}	
			}
			else if(strcmp(son_choose,"5")==0)
			{
				char name[50] = "邮箱";
				strcpy(chinese,name);
				for(num = 1; num<101; num++)
				{
					strcpy(information[num],inquire[num].email);
				}	
			}
			
			while(key==1)
			{
				ishave = 0;
				printf("请输入该联系人的%s：\n",chinese);
				scanf("%s",&evertype);
				for(num = 1; num<101&&key!=3; num++)
				{
					if(strcmp(evertype,information[num])==0)
					{
						ishave=1;
						printf("查找成功！\n");
						printf("请问是否查看详情？\n");	
						key = 3;
					}
				}
				if(ishave!=1) 
				{
					printf("查找失败~\n");
				}
				if(ishave==1)
				{
					printf("1.继续\n");
				printf("2.返回上一步\n");
				scanf("%s",choose);
				if((strcmp(choose,"1")==0)&&(ishave==1))
				{
					printf("名字：%s\n",inquire[num-1].name);  //因为上面的查找导致num大1，所以减掉 
					printf("职务：%s\n",inquire[num-1].job);
					printf("职称：%s\n",inquire[num-1].rank);		
					printf("手机：%s\n",inquire[num-1].tele);
					printf("邮箱：%s\n",inquire[num-1].email);
					printf("输入任意键继续~\n");
					scanf("%s",choose);
					system("cls");
					key = 2;
				}
				if(strcmp(choose,"2")==0) 
				{
					key = 1;
				}
				}
				
			}
		}
	 	else if(strcmp(choose,"3")==0) 
		{
		 	key = 2;	
		 	system("cls");
		}
	}
}

void Inon(struct member *io)  	//该函数用于外部文件存档 
{
	char choose[100]; 	//用于记录选项 
	int num;		//必要的参数 
	FILE *information;  	//以下都是文件指针 
	int key = 1;		//函数能正常进行的“钥匙” 
	
	while(key==1)
	{	
		printf("请选择你需要的功能：\n");
		printf("1.存档\n");
		printf("2.读取\n");
		printf("3.返回主菜单\n");
		scanf("%s",choose);
	
		if(strcmp(choose,"1")==0)
		{
			information = fopen("information.txt","w");
			for(num = 1;num<101;num++)
			{
				fwrite(&io[num],sizeof(struct member),1,information); 
			}
			fclose(information);
			system("cls");
			printf("存档成功!\n"); 
			
			key = 2;
		} 
		else if(strcmp(choose,"2")==0)
		{
		
			information = fopen("information.txt","r");
			for(num = 1;num<101;num++)
			{
				fread(&io[num],sizeof(struct member),1,information);
			}
			fclose(information);
			system("cls");
			printf("读取成功!\n"); 
		
			key = 2;
		}
		else if(strcmp(choose,"3")==0)
		{
			key = 2;
			system("cls");
		}
		else
		{
			printf("您输入的有问题，请重试\n");
			key = 1;	
		} 
	} 
} 

void Setting(struct member *set,int *use_detail,int *use_save,int *use_read) 
{
	FILE *setting1; 	//第一个设置的外部文件 
	FILE *setting2;		//第二个设置的外部文件
	FILE *setting3;		//第三个设置的外部文件
	char choose[100] = "1";		//父选项
	char son_choose[100];		//子选项
	int key=1;		//函数能正常进行的“钥匙”
	
	while((strcmp(choose,"0")!=0)&&(key==1))
	{
		key = 1;
		printf("1.是否每步都要详细确认？(默认开启)\n");
		printf("2.是否每次启动都自动读档？(默认开启)\n");
		printf("3.是否每次退出都自动存档？(默认开启，且该退出指在菜单按0退出)\n");
		printf("0.返回主菜单\n");
		printf("请选择需要修改的选项：");
		scanf("%s",choose);
		if(strcmp(choose,"1")==0)
		{
			while(key==1)
			{
				printf("1.开启\n");
				printf("2.关闭\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0)
				{
					setting1 = fopen("setting1.txt","w");
					fputc('1',setting1);
					fclose(setting1);
					*use_detail = 1;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else if(strcmp(son_choose,"2")==0)
				{
					setting1 = fopen("setting1.txt","w");
					fputc('0',setting1);
					fclose(setting1);
					*use_detail = 0;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else
				{
					printf("请重新输入~\n");
				}
			}
		} 
		else if(strcmp(choose,"2")==0)
		{
			while(key==1)
			{
				printf("1.开启\n");
				printf("2.关闭\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0)
				{
					setting2 = fopen("setting2.txt","w");
					fputc('1',setting2);
					fclose(setting2);
					*use_save = 1;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else if(strcmp(son_choose,"2")==0)
				{
					setting2 = fopen("setting2.txt","w");
					fputc('0',setting2);
					fclose(setting2);
					*use_save = 0;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else
				{
					printf("请重新输入~\n");
				}
			}
		} 
		else if(strcmp(choose,"3")==0)
		{
			while(key==1)
			{
				printf("1.开启\n");
				printf("2.关闭\n");
				scanf("%s",son_choose);
				if(strcmp(son_choose,"1")==0)
				{
					setting3 = fopen("setting3.txt","w");
					fputc('1',setting3);
					fclose(setting3);
					*use_read = 1;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else if(strcmp(son_choose,"2")==0)
				{
					setting3 = fopen("setting3.txt","w");
					fputc('0',setting3);
					fclose(setting3);
					*use_read = 0;	//使修改完后立即生效 
					key = 0;
					system("cls");
					printf("修改成功！\n");
				}
				else
				{
					printf("请重新输入~\n");
				}
			}
		} 
		else if(strcmp(choose,"0")==0)
		{
			key = 2;
			system("cls");
		}
		else
		{
			printf("请重新输入~\n");
			key = 1;
		}
		
	}
	
}
